package cn.fuxi2022.auth.service.impl;

import cn.fuxi2022.auth.properties.AuthProperties;
import cn.fuxi2022.auth.service.UriService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.AntPathMatcher;

import java.util.Objects;

@Service
public class UriServiceImpl implements UriService {

    /**
     * 路径匹配器，对URI字符串匹配
     */
    private final AntPathMatcher antPathMatcher = new AntPathMatcher();
    @Autowired
    private AuthProperties authProperties;

    @Override
    public boolean checkUri(String uri) {
        // 1.如果当前配置文件没有URI，说明请求都需要拦截
        if (Objects.isNull(authProperties.getUri())) {
            return false;
        }

        // 2.获取配置文件中的可以放行的URI
        for (String path : authProperties.getUri()) {
            if (antPathMatcher.match(path, uri)) {
                return true;
            }
        }
        return false;
    }
}
